package com.lz.ht.controller;

import com.lz.ht.model.*;
import com.lz.ht.page.PageModel;
import com.lz.ht.result.Result;
import com.lz.ht.service.RoomAdvanceService;
import com.lz.ht.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: RoomController
 * @Description:
 * @Author: CHonghao
 * @Date: 2021/7/9 11:26
 * @Version: V1.0
 **/
@Slf4j
@Controller
public class RoomController {

    @Autowired
    private RoomService roomServiceImpl;

    @Autowired
    private RoomAdvanceService roomAdvanceServiceImpl;

    @RequestMapping(value = "/room/list", method = {RequestMethod.GET})
    public String room_list() throws Exception {
        return "room/room_list";
    }


    @RequestMapping(value = "/room/list", method = {RequestMethod.POST})
    @ResponseBody
    public PageModel roomlist(Room room, PageModel<Room> page) throws Exception {
        page.init();
        List<Room> list = roomServiceImpl.findPageList(page, room);
        long count = roomServiceImpl.findCount(room);
        page.packData(count, list);
        return page;
    }

    @RequestMapping(value = "/room/add", method = {RequestMethod.GET})
    public String addInit(Room room, Model model) {
        return "room/room_add";
    }

    @RequestMapping(value = "/room/add", method = {RequestMethod.POST})
    @ResponseBody
    public Result add(Room room) {
        roomServiceImpl.add(room);
        return Result.genSuccessResult();
    }

    @RequestMapping(value = "/room/update",method = {RequestMethod.GET})
    public String updateInit(Room room, Model model){
        room = roomServiceImpl.findById(room.getRno());
        model.addAttribute("room",room);
        return "room/room_update";
    }

    @RequestMapping(value = "/room/update",method = {RequestMethod.POST})
    @ResponseBody
    public Result update(Room room){
        roomServiceImpl.updateById(room);
        return Result.genSuccessResult();
    }
    @RequestMapping(value = "/room/delete",method = {RequestMethod.POST})
    @ResponseBody
    public Result delete(Room room){
        roomServiceImpl.deleteById(room.getRno());
        return Result.genSuccessResult();
    }

    @RequestMapping(value = "/droproom/list", method = {RequestMethod.GET})
    public String drop_list() throws Exception {
        return "room/drop_list";
    }


    @RequestMapping(value = "/droproom/list", method = {RequestMethod.POST})
    @ResponseBody
    public PageModel booklist(Roombook roombook, PageModel<Roombook> page) throws Exception {
        page.init();
        List<Roombook> list = roomServiceImpl.findFreeRoomList(page, roombook);
        /*Session session = subject.getSession();
        Long userId = (Long) session.getAttribute("loginUserId");*/
        long count = roomServiceImpl.findFreeRoomCount(roombook);
        page.packData(count, list);
        return page;
    }

    @RequestMapping(value = "/droproom/drop",method = {RequestMethod.GET})
    public String droproomInit(String rno, String cid, String btime, Model model){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Long userId = (Long) session.getAttribute("loginUserId");
        btime=btime.substring(0,10);

        //获取当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String etime = df.format(new Date());// new Date()为获取当前系统时间
        DropInformation dropInformation = new DropInformation();
        dropInformation.setRno(rno);
        dropInformation.setCid(cid);
        dropInformation.setUserId(userId);
        dropInformation.setBtime(btime);
        dropInformation.setEtime(etime);

        dropInformation = roomServiceImpl.dropRoom(dropInformation);

        model.addAttribute("dropInformation",dropInformation);
        return "room/drop_information";
    }

    @RequestMapping(value = "/book/list",method = {RequestMethod.GET})
    public String book_list()throws Exception{
        return "room/book_list";
    }



    @RequestMapping(value = "/book/list",method = {RequestMethod.POST})
    @ResponseBody
    public PageModel booklist(Room room, PageModel<Room> page)throws Exception{
        page.init();
        List<Room> list = roomAdvanceServiceImpl.findPageListByDto(page, room);
        long count = roomAdvanceServiceImpl.findCountByDto(room);
        page.packData(count,list);
        return page;
    }


    @RequestMapping(value = "/book/add",method = {RequestMethod.GET})
    public String addInit(String rno, String btime, String etime, Model model) throws ParseException {
        Book book = new Book();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Long userId = (Long) session.getAttribute("loginUserId");
        book.setUserid(Math.toIntExact(userId));
        book.setRno(rno);
        /*SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd ");
        Date bdate = formatter.parse(btime);
        Date edate = formatter.parse(etime);*/
        book.setBtime(btime);
        book.setEtime(etime);
        model.addAttribute("book",book);
        return "room/book_add";
    }

    @RequestMapping(value = "/book/add",method = {RequestMethod.POST})
    @ResponseBody
    public Result add(Book book){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Long userId = (Long) session.getAttribute("loginUserId");
        book.setUserid(Math.toIntExact(userId));
        roomServiceImpl.addbook(book);
        return Result.genSuccessResult();
    }
}
